<?php

class ConfigController extends BackendController
{
    public $active = "systemconfig";

    public function actionIndex()
    {

        $actionType = isset($_REQUEST['actionType']) ? $_REQUEST['actionType'] : null;
        if ($actionType == 'insert') {
            $cfg_key = $_REQUEST['cfg_key'];
            $cfg_value = $_REQUEST['cfg_value'];
            $this->connection->createCommand("insert into rk_config (cfg_key,cfg_value,cfg_type,ismust) values (:cfg_key,:cfg_value,'customize',0)")
                ->bindParam(":cfg_key",$cfg_key)
                ->bindParam(":cfg_value",$cfg_value)
                ->execute();
        } else if ($actionType == 'delete') {
            $cfg_id = $_REQUEST['cfg_id'];
            $this->connection->createCommand("delete from rk_config where cfg_id = :cfg_id")
                ->bindParam(":cfg_id",$cfg_id)
                ->execute();
        } else if ($actionType == 'update') {
            $cfg_id = $_REQUEST['cfg_id'];
            $config = $this->connection->createCommand("select * from rk_config where cfg_id = :cfg_id")
                ->bindParam(":cfg_id",$cfg_id)
                ->queryRow();
            $this->data['updateconfig'] = $config;
        } else if ($actionType == 'updatedo') {
            $cfg_id = $_REQUEST['cfg_id'];
            $cfg_key = isset($_REQUEST['cfg_key'])?$_REQUEST['cfg_key']:null;
            $cfg_value = $_REQUEST['cfg_value'];
            if($cfg_key!=null){
                $this->connection->createCommand("update rk_config set cfg_key = :cfg_key,cfg_value=:cfg_value where cfg_id = :cfg_id")
                    ->bindParam(":cfg_id",$cfg_id)
                    ->bindParam(":cfg_key",$cfg_key)
                    ->bindParam(":cfg_value",$cfg_value)
                    ->execute();
            }else{
                $this->connection->createCommand("update rk_config set cfg_value=:cfg_value where cfg_id = :cfg_id")
                    ->bindParam(":cfg_id",$cfg_id)
                    ->bindParam(":cfg_value",$cfg_value)
                    ->execute();
            }

        }

        $configList = $this->connection->createCommand("select * from rk_config order by cfg_id asc,cfg_type asc,cfg_key asc")->queryAll();
        $this->data['configList'] = $configList;

        $this->render("index", $this->data);
    }
}